﻿
using System;
using Report.Entity;
using Report.IRepository;
using System.Linq.Expressions;
using System.Collections.Generic;

namespace Report.Repository
{
    public class StorageHeadDAL : BaseDAL<成品出入库主表>, IBase<成品出入库主表>
    {
        public bool inStorage(成品出入库主表 headEntity, 成品出入库子表 detailEntity, 在制流转条码 barcodeEntity, 在制流转条码记录 recEntity)
        {
            try
            {
                db.Ado.BeginTran();
                //新增头
                db.Insertable(headEntity).ExecuteCommand();
                //新增子表
                db.Insertable(detailEntity).ExecuteCommand();
                //更新在制流转条码
                db.Updateable(barcodeEntity).ExecuteCommand();
                //新增在制流转条码记录
                db.Insertable(recEntity).ExecuteCommand();

                //库存更新
                //查询是否存在库存记录
                成品总库存 existsSotckEntity = db.Queryable<成品总库存>().Where(q => q.总规格 == detailEntity.总规格).First();
                if (existsSotckEntity == null)
                {
                    //不存在，新增
                    成品总库存 newStockEntity = new 成品总库存();
                    newStockEntity.仓库类别编号 = headEntity.仓库类别编号;
                    newStockEntity.仓库编号 = headEntity.仓库编号;
                    newStockEntity.仓库核算类别 = headEntity.仓库核算类别;
                    newStockEntity.月份 = DateTime.Now.ToString("yyyyMM");
                    newStockEntity.产品系统编号 = detailEntity.产品系统编号;
                    newStockEntity.颜色 = detailEntity.颜色;
                    newStockEntity.自定义1 = detailEntity.自定义1;
                    newStockEntity.自定义2 = detailEntity.自定义2;
                    newStockEntity.自定义3 = detailEntity.自定义3;
                    newStockEntity.自定义4 = detailEntity.自定义4;
                    newStockEntity.自定义5 = detailEntity.自定义5;
                    newStockEntity.自定义6 = detailEntity.自定义6;
                    newStockEntity.自定义7 = detailEntity.自定义7;
                    newStockEntity.自定义8 = detailEntity.自定义8;
                    newStockEntity.库存自定义1 = detailEntity.库存自定义1;
                    newStockEntity.库存自定义2 = detailEntity.库存自定义2;
                    newStockEntity.库存自定义3 = detailEntity.库存自定义3;
                    newStockEntity.库存自定义4 = detailEntity.库存自定义4;
                    newStockEntity.库存自定义5 = detailEntity.库存自定义5;
                    newStockEntity.期初数量 = 0;
                    newStockEntity.入库数量 = detailEntity.数量;
                    newStockEntity.出库数量 = 0;
                    newStockEntity.结存数量 = detailEntity.数量;
                    newStockEntity.总规格 = detailEntity.总规格;
                    db.Insertable(newStockEntity).ExecuteCommand();
                }
                else
                {
                    //存在，更新
                    existsSotckEntity.入库数量 = existsSotckEntity.入库数量 + detailEntity.数量;
                    existsSotckEntity.结存数量 = existsSotckEntity.结存数量 + detailEntity.数量;
                    db.Updateable(existsSotckEntity).ExecuteCommand();
                }
                if (barcodeEntity.流程单号 != null && !"".Equals(barcodeEntity.流程单号)){
                    //更新流程单表
                    流程单表 liuchengbiao = db.Queryable<流程单表>().Where(q => q.流程单号 == barcodeEntity.流程单号).First();
                    liuchengbiao.完成数 = liuchengbiao.完成数 + barcodeEntity.数量;
                    if (liuchengbiao.数量 <= liuchengbiao.完成数) {
                        liuchengbiao.完成 = true;
                        liuchengbiao.完成人 = recEntity.创建人;
                        liuchengbiao.完成时间 = DateTime.Now;
                    }
                    db.Updateable(liuchengbiao).ExecuteCommand();
                    
                    //更新流程单工序表
                    流程单工序明细 liuchengmingxi = db.Queryable<流程单工序明细>().Where(q => q.流程单号 == barcodeEntity.流程单号 && q.工序系统编号==barcodeEntity.工序系统编号).First();
                    liuchengmingxi.发出量= liuchengmingxi.发出量+ barcodeEntity.数量;
                    liuchengmingxi.待流转量 = liuchengmingxi.合格产量 - liuchengmingxi.发出量;
                    db.Updateable(liuchengmingxi).ExecuteCommand();
                }
               
                db.Ado.CommitTran();
                return true;
            }
            catch(Exception ex)
            {
                db.Ado.RollbackTran();
                return false;
            }
        }
    }
}
